/*
 * CAnimation.cpp
 *
 *  Created on: 15/07/2008
 *      Author: Samuk
 */

#include <SDL/SDL.h>
#include "CAnimation.h"

CAnimation::CAnimation() {
	CurrentFrame = 0;
	MaxFrames = 0;
	FrameInc = 1;

	FrameRate = 100; //Milliseconds
	OldTime = SDL_GetTicks();

	Oscillate = false;
}

CAnimation::~CAnimation() {
	//
}

void CAnimation::OnAnimate() {
	if (((unsigned int) OldTime + FrameRate) > SDL_GetTicks()) {
		return;
	}

	OldTime = SDL_GetTicks();

	CurrentFrame += FrameInc;

	if (Oscillate) {
		if (FrameInc > 0) {
			if (CurrentFrame >= MaxFrames - 1) {
				FrameInc = -FrameInc;
			}
		} else {
			if (CurrentFrame <= 0) {
				FrameInc = -FrameInc;
			}
		}
	} else {
		if (CurrentFrame >= MaxFrames) {
			CurrentFrame = 0;
		}
	}
}

void CAnimation::SetFrameRate(int Rate) {
	FrameRate = Rate;
}

void CAnimation::SetCurrentFrame(int Frame) {
	if (Frame < 0 || Frame >= MaxFrames)
		return;

	CurrentFrame = Frame;
}

int CAnimation::GetCurrentFrame() {
	return CurrentFrame;
}
